<?php

require_once '../application/modules/admin/controllers/SuperControllerAdmin.php';

class Admin_ShortStoriesController extends Admin_SuperController {

    public function indexAction() {
        if ($this->getRequest()->isXmlHttpRequest()) {
            $this->_helper->layout->disableLayout();
            if ($this->_request->event == 'removeRow')
                $this->onRemoweRow($this->_request->id);
            if ($this->_request->event == 'updatePage')
                $this->onUpdatePage($this->getRequest()->getParams());
            if ($this->_request->event == 'createPage')
                $this->onCreatePage($this->getRequest()->getParams());
        }
        //_debug($this->getRequest()->getParams());
        $this->view->order_field = isset($this->_request->order_field) ? $this->_request->order_field : 'id';
        $this->view->order_dir = isset($this->_request->order_dir) ? $this->_request->order_dir : 'DESC';

        $this->view->tableColumn = array(
            0 => array(
                'title' => 'title',
                'field' => 'title',
                'wrap' => 'div150'
            ),
            1 => array(
                'title' => 'type',
                'field' => 'type',
            ),
            2 => array(
                'title' => 'content',
                'field' => 'content',
                'wrap' => 'div300'
            ),
            4 => array(
                'title' => 'active',
                'field' => 'active',
            ),
            9 => array(
                'title' => 'Change',
            ),
        );

        $view_page = new Zend_Session_Namespace('view_apage');
        $onPage = $view_page->on_page;
        //$onPage = 4;
        $page = (int) $this->_request->page;
        if (empty($page))
            $page = 1;

        $CONTM = new Admin_Model_Content();
        $qAr = array(
            'table_name' => 'short_stories',
            'order_field' => $this->view->order_field,
            'content' => $this->_request->title_cat,
            'type_filter' => $this->_request->type_filter,
            'order_dir' => $this->view->order_dir,
            'offset' => ($page - 1) * $onPage,
            'limit' => $onPage,
        );
        $count_rows = 0;
        $this->view->Info = $CONTM->getItems($qAr, $count_rows);
        $this->view->countRow = $count_rows; // $listModel->getCountRows($qAr);

        $paginator = Zend_Paginator::factory((int) $this->view->countRow);
        $paginator->setDefaultItemCountPerPage($onPage);
        $paginator->setCurrentPageNumber($page);

        $this->view->paginator = $paginator;
        $this->view->onPage = $onPage;

        $this->view->info = array();
        $this->view->info['id'] = 0;
        $this->view->info['event'] = 'createPage';
        $this->view->info['fields'] = $this->sructure;
        $SSTRM = new Application_Model_ShortStories();
        $this->view->types = $SSTRM->getShortStoriesTypes();
    }

    protected $sructure = array(
        0 => array('name' => 'title', 'id' => 'title', 'type' => 'str', 'class' => 'send-val'),
        1 => array('name' => 'public_date', 'type' => 'date', 'class' => 'send-val'),
        4 => array('name' => 'type', 'id' => "type", 'type' => 'build', 'function' => 'getShortStoriesSelect'),
        5 => array('name' => 'category_id', 'id' => "category_id", 'type' => 'build', 'function' => 'getShortStoriesCategoriesSelect'),
        9 => array('name' => 'content', 'id' => 'ckeditor', 'type' => 'text'),
        10 => array('name' => 'active', 'id' => 'active', 'type' => 'one-checkbox'),
    );

    public function getPageInfoAction() {
        Zend_Layout::getMvcInstance()->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        $page_id = (int) $this->_request->page_id;
        if (!empty($page_id)) {
            $CONTM = new Admin_Model_Content();
            $info = $CONTM->getOneObj('short_stories', $page_id);
            $info['content'] = stripcslashes($info['content']);
            $info['labels'] = array();
            $date = new DateTime($info['public_date']);
            $info['public_date'] = $date->format('Y-m-d');
            if (is_array($info)) {
                echo json_encode($info);
            }
        } else {
            throw new Exception('no parameters required');
        }
    }

    protected function onRemoweRow($id) {
        if (!empty($id)) {
            $ACNTM = new Admin_Model_Content();
            $ACNTM->removeRow('short_stories', $id);
        }
    }

    protected function onUpdatePage($data) {
        if (!empty($data)) {
            $ACNTM = new Admin_Model_Content();
            $ACNTM->update('short_stories', $data,1);
        }
    }

    protected function onCreatePage($data) {
        if (!empty($data)) {
            $ACNTM = new Admin_Model_Content();
            $page_id = $ACNTM->insert('short_stories', $data);
        }
    }

}
